<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>18</title>
    <script src="vue.js"></script>
</head>

<body>
    <div id="app">
        商品名：<input type="name" v-model="name" name="" id=""><br>
        单价：<input type="price" v-model="price" name="" id=""><br>
        数量：<input type="num" v-model="num" name="" id=""><br>
        <button @click="add">添加</button>
        <ul>
            <li v-for="item in list">
                {{item.name}}的数量是{{item.num}},价格为{{item.price*item.num}}
            </li>
            <li>
                商品的数量为{{sp_num}}，总价格{{pr_sum}}元
            </li>
        </ul>
    </div>
    <script>
        var app = new Vue({
            el:'#app',
            data: {
                list:[]
            },
            methods:{
                add(){
                    this.list.push({'name':this.name,'price':this.price,'num':this.num}),
                    this.name='',
                    this.price='',
                    this.num=''
                }
            },
            computed:{
                pr_sum(){
                    return this.list.reduce((total,b)=>{
                        return total + b.price*b.num
                    },0)
                },
                sp_num(){
                    return this.list.reduce((total,b)=>{
                        return total + b.num*1
                    },0)
                }
            }
        });

    </script>
</body>

</html>